require("../util.js");
require("../component/components.js");
require("../component/selfmediaflow.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
var dataList=JSON.parse(sessionStorage.getItem("data"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#mediaScreen",
    data:{
        loadFlag:false,
        type:"",
        categoryId:"",
        name:"",
        pageNumber:1,
        categoryQuery:dataList,
        queryList:{},
        queryList1:[],
        queryList2:[]//筛选结果
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.type=this.searchObject&&this.searchObject.type;
        if(this.type==1||this.type==2){    //自媒体
            this.name="自媒体筛选"
        }else if(this.type==3){
            this.name="圈子筛选";
        }
        this.getCategorySearchAdvList();
    },
    methods:{
        //后退
        getBack:function(){
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //获取筛选配置信息
        getMainAllCategoryList:function(){
            var that=this;
            this.ajax({url:"/selfMedia/getMainAllCategoryList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.categoryQuery=b.json.data;
                    console.log(that.categoryQuery)
                    // that.getCategory();
                    if(that.categoryQuery.regionalQuery==1){
                        // that.getChinaList();
                        // that.getCountryStateList(1);
                        that.getCategorySearchAdvList();
                    }

                }else{
                    vm.$refs.prompt.toast("获取筛选配置信息失败！")
                }
            });
        },
        getTab:function(id){
            var that=this;
            console.log(id)
            that.queryList={};
            that.queryList1=[];
            that.queryList2=[];
            that.pageNumber=1;
            that.categoryId=id;
            that.getCategorySearchAdvList();
        },
        //筛选
        getCategorySearchAdvList:function(){
            //console.log(id);
            var that=this,url,data;
            data={
                pageNumber:that.pageNumber
            };
            if(that.categoryId){
                data.categoryId=that.categoryId;
            }
            console.log(data)
            if(that.type==1||that.type==2){
                url="/selfMedia/getCategorySearchAdvList";
            }else if(that.type==3){
                url="/selfMedia/getMediaSearchCirclesList";
            }
            this.ajax({url:url,data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.queryList=b.json.data;
                    that.queryList1=that.queryList.list;
                    if(that.queryList.pageNumber>1){
                        if(that.queryList.list.length>0) {
                            that.queryList2 = that.queryList2.concat(b.json.data.list);
                        }
                    }else{
                        that.queryList2= that.queryList1;
                    }
                    vm.loadFlag=false;
                    //lazyload();
                }else{
                    that.queryList={};
                    that.queryList1=[];
                    that.queryList2=[];
                    vm.$refs.prompt.toast("获取人气列表失败！")
                }
                //console.log(that.queryList2)
            });
        }
    }
    ,watch:{
        queryList2:function(){
            if(this.queryList2.length!=0){
                this.$nextTick(function(){
                    fmasonry();
                    $("img.lazy").lazyload({
                        failurelimit:10,
                        load:fmasonry
                    });
                    console.log($(".selfMedia-tabCent").height());
                });
            }
        },
        countryList:function(){
            var that=this;
            that.countryList.forEach(function(val){
                that.countryCityIdList.push(val);
            });
            //console.log(this.countryCityIdList)
        }
    }
});
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    var loadFlag = vm.queryList.lastPage;
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.pageNumber++;
            vm.getCategorySearchAdvList();
        }else{
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};
function fmasonry() {
    minigrid('.selfMedia-tabCent', '.selfMedia-list',9);
    // $(".selfMedia-tabCent").masonry({
    //     gutterWidth: 10,
    //     itemSelector: '.selfMedia-list',
    //     isAnimated: true,
    //     isFitWidth:true
    // });
}
function lazyload(){
    $("img.lazy").lazyload();
}